package com.company.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.base.common.persistence.Page;
import com.base.model.ViewSelectModel;
import com.base.util.JsonUtil;
import com.company.model.CompanyEmployee;
import com.company.model.CompanyEmployeeFee;
import com.company.model.CompanyProject;
import com.company.service.EmployeeFeeService;
import com.company.service.EmployeeService;

@Controller
public class EmployeeController {
	public static final String toEmployeeView = "/company/employee";
	public static final String employeeList = "/company/employee/list";
	public static final String employeeSave = "/company/employee/save";
	public static final String openEmployeeEditView = "/company/employee/editView";
	public static final String openEmployeeFeeEditView = "/company/employeeFee/editView";
	public static final String openEmployeeFeeAddView = "/company/employeeFee/addView";
	
	
	public static final String employeeDelete = "/company/employee/delete";
	public static final String employeeSelect = "/company/employee/select";

	public static final String toEmployeeFeeView = "/company/employee/fee";
	public static final String employeeFeeList = "/company/employee/fee/list";
	public static final String employeeFeeEdit = "/company/employee/fee/edit";
	public static final String employeeFeeDelete = "/company/employee/fee/delete";
	public static final String employeeFeeSave = "/company/employeeFee/save";
	// 页面路径
	public static final String employeePage = "/company/employee";
	public static final String employeeListPage = "/company/employeeList";
	public static final String employeeEditPage = "/company/employeeEdit";
	public static final String employeeFeePage = "/company/employeeFee";
	public static final String employeeFeeListPage = "/company/employeeFeeList";
	public static final String employeeFeeEditPage = "/company/employeeFeeEdit";
	public static final String employeeFeeAddPage = "/company/employeeFeeAdd";
	
	
	@Autowired
	EmployeeService employeeService;
	@Autowired
	EmployeeFeeService employeeFeeService;

	@RequestMapping(value = toEmployeeView, method = RequestMethod.POST)
	public ModelAndView toEmployeeView(HttpServletRequest req, HttpServletResponse res) {
		ModelAndView mv = new ModelAndView(employeePage);
		String companyId = req.getSession().getAttribute("companyId")
				.toString();
		List<CompanyEmployee> list = null;
		try {
			list = employeeService.findEmployeeListByCompanyId(companyId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mv.addObject("employeeList", list);
		return mv;
	}

	@ResponseBody
	@RequestMapping(value = employeeList, method = RequestMethod.POST)
	public ModelAndView employeeList(HttpServletRequest req, HttpServletResponse res) {
		ModelAndView mv = new ModelAndView(employeeListPage);
		String companyId = req.getSession().getAttribute("companyId")
				.toString();
		List<CompanyEmployee> list = null;
		try {
			list = employeeService.findEmployeeListByCompanyId(companyId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("employeeList", list);
		return mv;
	}

	@ResponseBody
	@RequestMapping(value = employeeSave, method = RequestMethod.POST)
	public boolean employeeSave(HttpServletRequest req,
			HttpServletResponse res,  CompanyEmployee companyEmployee) {
		String companyId = req.getSession().getAttribute("companyId")
				.toString();
		boolean result = false;
		try {
			companyEmployee.setCompanyId(companyId);
			result = employeeService.saveEmployee(companyEmployee);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = employeeFeeSave, method = RequestMethod.POST)
	public boolean employeeFeeSave(HttpServletRequest req,
			HttpServletResponse res, CompanyEmployeeFee companyEmployeeFee) {
		String companyId = req.getSession().getAttribute("companyId")
				.toString();
		boolean result = false;
		try {
			companyEmployeeFee.setCompanyId(companyId);
			result = employeeFeeService.saveEmployeeFee(companyEmployeeFee);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = openEmployeeEditView, method = RequestMethod.POST)
	public ModelAndView openEmployeeEditView(HttpServletRequest req,
			HttpServletResponse res, String id) {
		ModelAndView mv = new ModelAndView(employeeEditPage);
		CompanyEmployee result = null;
		try {
			result = employeeService.findEmployeeById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("employee", result);
		return mv;
	}
	
	@ResponseBody
	@RequestMapping(value = openEmployeeFeeEditView, method = RequestMethod.POST)
	public ModelAndView openEmployeeFeeEditView(HttpServletRequest req,
			HttpServletResponse res, String id) {
		String companyId = req.getSession().getAttribute("companyId")
				.toString();
		ModelAndView mv = new ModelAndView(employeeFeeEditPage);
		CompanyEmployeeFee result = null;
		try {
			result = employeeFeeService.findEmployeeFeeById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<CompanyEmployee> list = null;
		try {
			list = employeeService.findEmployeeListByCompanyId(companyId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mv.addObject("employeeFee", result);
		mv.addObject("employeeList", list);
		return mv;
	}
	
	@ResponseBody
	@RequestMapping(value = openEmployeeFeeAddView, method = RequestMethod.POST)
	public ModelAndView openEmployeeFeeAddView(HttpServletRequest req,
			HttpServletResponse res, String id) {
		String companyId = req.getSession().getAttribute("companyId")
				.toString();
		ModelAndView mv = new ModelAndView(employeeFeeAddPage);
		CompanyEmployeeFee result = null;
		try {
			result = employeeFeeService.findEmployeeFeeById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<CompanyEmployee> list = null;
		try {
			list = employeeService.findEmployeeListByCompanyId(companyId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mv.addObject("employeeFee", result);
		mv.addObject("employeeList", list);
		return mv;
	}

	@ResponseBody
	@RequestMapping(value = employeeDelete, method = RequestMethod.POST)
	public boolean employeeDelete(String ids) {
		boolean result = false;
		try {
			result = employeeService.deleteEmployeeByIds(ids);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@RequestMapping(value = toEmployeeFeeView, method = RequestMethod.POST)
	public ModelAndView toEmployeeFeeView(HttpServletRequest req,
			HttpServletResponse res) {
		ModelAndView mv = new ModelAndView(employeeFeePage);
		String companyId = req.getSession().getAttribute("companyId")
				.toString();
		List<CompanyEmployeeFee> list = null;
		try {
			list = employeeFeeService.findEmployeeFeeByCompanyId(companyId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		mv.addObject("employeeFeeList", list);
		return mv;
	}

	@ResponseBody
	@RequestMapping(value = employeeFeeList, method = RequestMethod.POST)
	public ModelAndView employeeFeeList(HttpServletRequest req,
			HttpServletResponse res) {
		ModelAndView mv = new ModelAndView(employeeFeeListPage);
		String companyId = req.getSession().getAttribute("companyId")
				.toString();
		List<CompanyEmployeeFee> list = null;
		try {
			list = employeeFeeService.findEmployeeFeeByCompanyId(companyId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("employeeFeeList", list);
		return mv;
	}

	@ResponseBody
	@RequestMapping(value = employeeFeeEdit, method = RequestMethod.POST)
	public boolean employeeFeeEdit(HttpServletRequest req,
			HttpServletResponse res, CompanyEmployeeFee companyEmployeeFee) {
		String companyId = req.getSession().getAttribute("companyId")
				.toString();
		boolean result = false;
		try {
			if ("_empty".equals(companyEmployeeFee.getId())) {
				companyEmployeeFee.setId(null);
			}
			companyEmployeeFee.setCompanyId(companyId);
			result = employeeFeeService.saveEmployeeFee(companyEmployeeFee);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@ResponseBody
	@RequestMapping(value = employeeFeeDelete, method = RequestMethod.POST)
	public boolean employeeFeeDelete(String ids) {
		boolean result = false;
		try {
			result = employeeFeeService.deleteEmployeeFeeByIds(ids);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
